package com.ikkong.shine.controller;

import java.util.Date;
import java.util.Map;

import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.toolbox.Func;
import com.ikkong.core.toolbox.Record;
import com.ikkong.shine.meta.intercept.TriageIntercept;
import com.ikkong.shine.model.Triage;
import com.ikkong.shine.service.TriageService;
import com.ikkong.shine.service.impl.TriageServiceImpl;
import com.ikkong.shine.tools.Shared;
import com.ikkong.system.controller.base.UrlPermissController;

/**
 * Generated by Blade.
 * 2017-03-17 11:02:35
 */
public class TriageController extends UrlPermissController {
	private static String CODE = "triage";
	private static String PERFIX = "triage";
	private static String LIST_SOURCE = "Triage.list";
	//private static String LIST_PAGER="Triage.listpager";
	private static String VIEW_SOURCE = "Triage.view";
	private static String BASE_PATH = "/platform/triage/";
	Shared shared=new Shared();
	
	TriageService service = new TriageServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "triage.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "triage_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Triage triage = service.findById(id);
		setAttr("model", JsonKit.toJson(triage));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "triage_edit.html");
	}

	public void view() {
		String id = getPara(0);
		@SuppressWarnings("unchecked")
		Map<String, Object> map=Blade.dao().selectSingle(VIEW_SOURCE, Record.create().set("triage_id", id), Map.class);
		if(null != map && null != map.get("triage_type")){
			map.put("triage_type", Func.getDictName(110, map.get("triage_type")));
		}
		if(null != map && null != map.get("pager_type")){
			map.put("pager_type", Func.getDictName(111, map.get("pager_type")));
		}
		if(null != map && null != map.get("reorder_type")){
			map.put("reorder_type", Func.getDictName(112, map.get("reorder_type")));
		}
		if(null != map && null != map.get("return_flag_step")){
			map.put("return_flag_step", Func.getDictName(113, map.get("return_flag_step")));
		}
		if(null != map && null != map.get("call_buffer")){
			map.put("call_buffer", Func.getDictName(114, map.get("call_buffer")));
		}
		setAttr("model", JsonKit.toJson(map));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "triage_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new TriageIntercept());
		renderJson(grid);
	}
	
	
	
	public void save() {
		Triage triage = mapping(PERFIX, Triage.class);
		
		Triage triage_name=service.findByName(triage.getName());
		if(null!=triage_name&&triage_name.getTriage_id()!=null){
			renderJson(error("分诊台名称已存在！"));
			return;
		}
		Triage triage_ip=service.findByIP(triage.getIp());
		if(null!=triage_ip&&triage_ip.getTriage_id()!=null){
			renderJson(error("当前IP:"+triage.getIp()+" 已存在！"));
			return;
		}
		if(shared.isIP(triage.getIp().toString())==false){
			renderJson(error("当前ip:"+triage.getIp()+"不合法！"));
			return;
		}
		
		triage.setCreate_time(new Date());//当前时间
		boolean temp = service.save(triage);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	
	public void update() {
		Triage triage = mapping(PERFIX, Triage.class);
		//根据分诊台名称查询
		Triage triage_name=service.findByName(triage.getName());
		if(null!=triage_name&&triage_name.getTriage_id().intValue()!=triage.getTriage_id().intValue()){
			renderJson(error("当前："+triage.getName()+"******已存在！"));
			return;
		}
		//根据分诊台IP查询
		Triage triage_ip=service.findByIP(triage.getIp());
		if(null!=triage_ip&&triage_ip.getTriage_id().intValue()!=triage.getTriage_id().intValue()){
			renderJson(error("当前："+triage.getIp()+"******已存在！"));
			return;
		}
		boolean temp = service.update(triage);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
	
	public void triageQueuetype(){
		setAttr("code", CODE);
		setAttr("aid", getPara(0));
		render(BASE_PATH+"triage_queutype.html");
	}
	
	public void triagePager(){
		setAttr("code", CODE);
		setAttr("aid", getPara(0));
		render(BASE_PATH+"triage_pager.html");
	}

}
